package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.Utils;
import com.ludomancy.sos64.Accumulator;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;
import com.ludomancy.sos64.Sos64App;

public class Adult64 extends Component
{
	private ImageAnimation egg;
	private boolean consuming = false;
	private Accumulator fadeTimer = new Accumulator(1800);
	
	public Adult64(Point pos)
	{
		super(pos.x,pos.y);
		if(egg == null)
		{
			egg = new ImageAnimation(new ImageStrip(ResourceManager.getImage("bigegg.png"), 45, 47, 10), 7);
		}
	}
	
	public Point getSize()
	{
		return new Point(45,47);
	}

	public void paint(Graphics g, int px, int py)
	{
		if(fadeTimer.isRunning())
		{
			fadeTimer.update();
			Utils.pushAlpha(g, fadeTimer.getRatio());
		}
		egg.paintCurrentFrame(g, px, py);
		if(fadeTimer.isRunning())
		{
			Utils.popAlpha(g);
		}

	}
	
	public void onMoved()
	{
		Point tile = Game.game.tileFor(getHotspot());
		if(Game.game.terrain.isWater(tile.x, tile.y))
		{
			consuming = true;
			Sound.play("go64.wav");
			egg.play();
		}
	}
	
	public boolean canMove()
	{
		return fadeTimer.isDone();
	}

	public void update()
	{
		egg.update();
		fadeTimer.update();
		if(consuming)
		{
			Game.game.poison();
			if(egg.isOver())
			{
				Sos64App.mainWidget.endGame();
				Game.game.components.remove(this);
			}
		}
	}
}
